﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using DataLayer;
using AvalonDock;
using PixelLab.Wpf;
using PixelLab.Common;
using System.Windows.Forms;
using System.Collections.ObjectModel;
using System.Windows.Interop;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Drawing.Imaging;
using System.Net;
using System.Text.RegularExpressions;
namespace LuanVanProject
{
    /// <summary>
    /// Interaction logic for DetailViewContent.xaml
    /// </summary>
    public partial class DetailViewContent : System.Windows.Controls.UserControl
    {
        public class ImageTag
        {
            public NodeContent nodeContent;
            public DocumentContainForViewer viewer;
        }
        static public DocumentPane dockPane;
        static public ProjectPane proPane;
        private Node currentNode;
        public DetailViewContent()
        {
            InitializeComponent();
          
        }
        public void SetUpComboBox()
        {
            txbIdea.Items.Clear();
            List<Node> list = new List<Node>();
            App.currentMindmap.RootNode.GetNodesAfter(list);
            ComboBoxItem rootitem = new ComboBoxItem();
            rootitem.Content = App.currentMindmap.RootNode.Name;
            txbIdea.Items.Add(rootitem);
            foreach (Node node in list)
            {
                ComboBoxItem item = new ComboBoxItem();
                item.Content = node.Name;
                txbIdea.Items.Add(item);
            }
            //txbIdea.SelectedIndex = 0;
        }
        public void SetContain(Node node)
        {
            this.Tag = node;
            this.currentNode = node;
            txbIdea.Text = node.Name;
          //  StackPanelContent.Children.Clear();
            MyListBox.Items.Clear();
                for (int i = 0; i < node.ContentList.Count; i++)
                {
                    NodeContent con=node.ContentList[i];
                    con.Parent = currentNode;
                    if (con.Type.ToLower() == "image")
                    {
                        ImageTag imgTag=new ImageTag();
                        imgTag.nodeContent = con;
                        imgTag.viewer = null;
                        ImageContent imgCon = con as ImageContent;
                        PictureContentView img = new PictureContentView();
                        img.SetImageSource(imgCon.Content);
                        
                        this.MyListBox.Items.Add(img);
                        img.Tag = imgTag;
                        img.ParentNode = currentNode;
                        img.MouseDoubleClick += new MouseButtonEventHandler(img_DoubleClick);
                        img.DetailView = this;
                    }
                    if (con.Type.ToLower() == "text")
                    {
                        ImageTag imgTag = new ImageTag();
                        imgTag.nodeContent = con;
                        imgTag.viewer = null;
                        TextContent text = con as TextContent;
                        string content = "";
                        foreach (DataLayer.Paragraph paragraph in text.ListParagraph)
                        {
                            content += paragraph.Content+"\n";
                        }
                        TextContentView tcv = new TextContentView(content);
                        MyListBox.Items.Add(tcv);
                        tcv.Tag = imgTag;
                        tcv.ParentNode = currentNode;
                        tcv.MouseDoubleClick += new MouseButtonEventHandler(tcv_MouseDoubleClick);
                        tcv.DetailView = this;
                    }
                }
                if (node.ContentList.Count == 0)
                {
                    AdviserControl advise = new AdviserControl();
                    MyListBox.Items.Add(advise);
                }
        }
        void tcv_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if ((sender as TextContentView).Parent != null)
            {
                TextContentView tcv = sender as TextContentView;
                ImageTag tag = ((tcv).Tag) as ImageTag;
                TextContent tc = tag.nodeContent as TextContent;
                if (tag.viewer == null || tag.viewer.Parent == null)
                {
                    TextEditor te = new TextEditor(tc.ListParagraph[0].Content);
                    te.ShowDialog();
                    if (te.result == true)
                    {
                        tcv.SetText(te.output);
                        tc.ListParagraph[0].Content = te.output;
                    }
                }
                else
                {
                    dockPane.SelectedItem = tag.viewer;
                }
            }
        }
        void img_DoubleClick(object sender, MouseButtonEventArgs e)
        {
            if ((sender as PictureContentView).Parent != null)
            {
                ImageTag tag = ((sender as PictureContentView).Tag) as ImageTag;
                if (tag.viewer == null || tag.viewer.Parent == null)
                {
                    DocumentContainForViewer docContain = DocumentContainForViewer.OpenFile((tag.nodeContent as ImageContent).ImagePath);
                    dockPane.Items.Add(docContain);
                    dockPane.SelectedItem = docContain;
                    tag.viewer = docContain;
                }
                else
                {
                    dockPane.SelectedItem = tag.viewer;
                }
            }
        }
        private void MyListBox_ReorderRequested(object sender, ReorderEventArgs args)
        {
             //sender as Picture
            var reorderListBox = (ReorderListBox)args.OriginalSource;
            var senditem= reorderListBox.ItemContainerGenerator.ItemFromContainer(args.ItemContainer);
            if ((senditem as PictureContentView)!=null)
            {
                PictureContentView pic = senditem as PictureContentView;
                ImageTag tag= pic.Tag as ImageTag;
                NodeContent senderContent=tag.nodeContent;
                var toPictureContentView = MyListBox.ItemContainerGenerator.ItemFromContainer(args.ToContainer) as PictureContentView;
                if (toPictureContentView != null)
                {
                    ImageTag toTag = toPictureContentView.Tag as ImageTag;
                    int oldIndex = currentNode.ContentList.IndexOf(senderContent);
                    int newIndex = currentNode.ContentList.IndexOf(toTag.nodeContent);
                    currentNode.ContentList.RemoveAt(oldIndex);
                    currentNode.ContentList.Insert(newIndex, senderContent);
                    this.SetContain(currentNode);
                }
                else
                {
                    var toTextContentView = MyListBox.ItemContainerGenerator.ItemFromContainer(args.ToContainer) as TextContentView;
                    ImageTag toTag = toTextContentView.Tag as ImageTag;
                    int oldIndex = currentNode.ContentList.IndexOf(senderContent);
                    int newIndex = currentNode.ContentList.IndexOf(toTag.nodeContent);
                    currentNode.ContentList.RemoveAt(oldIndex);
                    currentNode.ContentList.Insert(newIndex, senderContent);
                    this.SetContain(currentNode);
                
                }
            }
            if ((senditem as TextContentView)!=null)
            {
                TextContentView text = senditem as TextContentView;
                ImageTag tag = text.Tag as ImageTag;
                NodeContent senderContent = tag.nodeContent;
                var toPictureContentView = MyListBox.ItemContainerGenerator.ItemFromContainer(args.ToContainer) as PictureContentView;
                if (toPictureContentView != null)
                {
                    ImageTag toTag = toPictureContentView.Tag as ImageTag;
                    int oldIndex = currentNode.ContentList.IndexOf(senderContent);
                    int newIndex = currentNode.ContentList.IndexOf(toTag.nodeContent);
                    currentNode.ContentList.RemoveAt(oldIndex);
                    currentNode.ContentList.Insert(newIndex, senderContent);
                    this.SetContain(currentNode);
                }
                else
                {
                    var toTextContentView = MyListBox.ItemContainerGenerator.ItemFromContainer(args.ToContainer) as TextContentView;
                    ImageTag toTag = toTextContentView.Tag as ImageTag;
                    int oldIndex = currentNode.ContentList.IndexOf(senderContent);
                    int newIndex = currentNode.ContentList.IndexOf(toTag.nodeContent);
                    currentNode.ContentList.RemoveAt(oldIndex);
                    currentNode.ContentList.Insert(newIndex, senderContent);
                    this.SetContain(currentNode);

                }
            }
        }

        private void NewCommentButton_Click(object sender, RoutedEventArgs e)
        {
            //implement more textcontentviewer here
            if (currentNode != null)
            {
                TextEditor te = new TextEditor();
                try
                {
                    te.ShowDialog();
                    if (te.result)
                    {
                        TextContentView tcv = new TextContentView(te.output);
                        TextContent textContent = new TextContent();
                        textContent.Type = "text";
                        textContent.AddParagraph(new DataLayer.Paragraph(te.output));
                        ImageTag imgTag = new ImageTag();
                        imgTag.nodeContent = textContent;
                        imgTag.viewer = null;
                        tcv.Tag = imgTag;
                        tcv.MouseUp += new MouseButtonEventHandler(tcv_MouseDoubleClick);
                        currentNode.AddContent(textContent);
                        this.SetContain(currentNode);
                    }
                }
                catch { }

            }
            else
            {
                System.Windows.MessageBox.Show("Please select an idea");
            }

        }

        private void NewImageButton_Click(object sender, RoutedEventArgs e)
        {
            //implement insertpicture here
            if (currentNode == null)
            {
                System.Windows.MessageBox.Show("please select an idea");
            }
            else
            {
                OpenFileDialog op = new OpenFileDialog();
                op.Filter = "Image Files (*.bmp; *.png; *.jpg;*.gif)|*.bmp; *.png;*.jpg;*.gif";
                if (op.ShowDialog() == DialogResult.OK)
                {
                    string filename = op.FileName;
                    DateTime now = DateTime.Now;
                    string newFileName = "img" + now.Year.ToString() + now.Month.ToString() + now.Hour.ToString() + now.Minute.ToString() + now.Second.ToString() + System.IO.Path.GetExtension(filename);
                    System.IO.File.Copy(filename, App.ProjectDirectory + "/Images/" + newFileName);
                    string packUri = App.ProjectDirectory + "\\Images/" + newFileName;
                    //ResourcesHelper.GetInstance().AddResource(packUri);
                    //int id = ResourcesHelper.GetInstance().PathToID(packUri);
                    ImageContent imgCon = new ImageContent(packUri);
                    imgCon.Type = "image";
                    currentNode.ContentList.Add(imgCon);
                    SetContain(currentNode);
                }
            }
        }

        private void MyListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }
        private void txbIdea_TextInput(object sender, TextCompositionEventArgs e)
        {
            Node n = this.Tag as Node;
            n.Name = txbIdea.Text;
            // proPane.editor.ViewModel.Update();
            App.Synchronize();

        }

        private void txbIdea_TextChanged(object sender, TextChangedEventArgs e)
        {

        }

        private void txbIdea_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                /*
                Node n = this.Tag as Node;
                n.Name = txbIdea.Text;
                // proPane.editor.ViewModel.Update();
                App.Synchronize();*/
                Node n = App.currentMindmap.GetNodeByName(txbIdea.Text);
                if (n != null)
                    this.SetContain(n);
            }
        }

        private void txbIdea_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if ((txbIdea.SelectedItem as ComboBoxItem) != null)
            {
                Node n = App.currentMindmap.GetNodeByName((txbIdea.SelectedItem as ComboBoxItem).Content.ToString());
                if (n != null)
                    this.SetContain(n);
            }
        }

        private void CaptureScreenEvent(object sender, RoutedEventArgs e)
        {
            ShadowWindow.CaptureScreen(App.ProjectDirectory + "\\" + "Images");
            if (ShadowWindow.returnImageContent != null)
            {
                currentNode.AddContent(ShadowWindow.returnImageContent);
                App.Synchronize();
                this.SetContain(currentNode);
            }
            
        }

        private void Grid_Drop_2(object sender, System.Windows.DragEventArgs e)
        {
           GetImageSourceFromDropData(e);
        }
        private void GetImageSourceFromDropData(System.Windows.DragEventArgs e)
        {
                if (e.Data.GetDataPresent(System.Windows.DataFormats.Rtf, true))
                {
                    string text = e.Data.GetData(System.Windows.DataFormats.Rtf, true).ToString();
                    TextContent txtContent = new TextContent();
                    DataLayer.Paragraph para = new DataLayer.Paragraph(text);
                    txtContent.Type = "text";
                    txtContent.ListParagraph.Add(para);
                    currentNode.AddContent(txtContent);
                    App.Synchronize();
                    this.SetContain(this.currentNode);
                    //App.InfoLabel.SetMessage("Drop Text Succesful", true);
                    return ;
                }
                if (e.Data.GetDataPresent(System.Windows.DataFormats.Bitmap, true))
                {
                    var bm = e.Data.GetData(System.Windows.DataFormats.Bitmap, true);
                    // This is an BitmapSource so we can just use it; we wouldn't usually see this if we 
                    // did something like e.Data.GetData("System.Windows.Media.Imaging.BitmapSource") and
                    // it succeeded.
                    Bitmap currentBitmap = bm as Bitmap;
                    DateTime inow = DateTime.Now;
                    var interopBitmap = bm as InteropBitmap;
                    string inewPath = App.ProjectDirectory+"/Images" + "/" + "abc" + inow.Second.ToString() + inow.Minute.ToString() + inow.Hour.ToString() + inow.Day.ToString() + inow.Month.ToString() + inow.Year.ToString() + ".png";
                    if (currentBitmap != null)
                    {
                        currentBitmap.Save(inewPath, ImageFormat.Png);
                        //int returnID = ResourcesHelper.GetInstance().AddResource(inewPath);
                        ImageContent imgContent = new ImageContent(inewPath);
                        imgContent.Type = "image";
                        this.currentNode.AddContent(imgContent);
                        this.SetContain(this.currentNode);
                        App.Synchronize();
                        //App.InfoLabel.SetMessage("Drop Image Successful", true);
                    }
                    return ;
                }
                else if (e.Data.GetDataPresent(System.Windows.DataFormats.FileDrop))
                {
                    var fileNames = (string[])e.Data.GetData(System.Windows.DataFormats.FileDrop, true);
                    Uri path=new Uri(Uri.EscapeUriString("file:///" + fileNames[0].Replace("\\", "/")));
                    var bmp = new BitmapImage(path);
                    MemoryStream mse = new MemoryStream();
                    System.Windows.Media.Imaging.BmpBitmapEncoder encoder = new System.Windows.Media.Imaging.BmpBitmapEncoder();
                    encoder.Frames.Add(BitmapFrame.Create(bmp));
                    encoder.Save(mse);
                    System.Drawing.Image currentBitmap = Bitmap.FromStream(mse);
                    DateTime inow = DateTime.Now;
                    string inewPath = App.ProjectDirectory + "/Images" + "/" + "abc" + inow.Second.ToString() + inow.Minute.ToString() + inow.Hour.ToString() + inow.Day.ToString() + inow.Month.ToString() + inow.Year.ToString() + ".png";
                    currentBitmap.Save(inewPath, ImageFormat.Png);
                    //int returnID = ResourcesHelper.GetInstance().AddResource(inewPath);
                    ImageContent imgContent = new ImageContent(inewPath);
                    imgContent.Type = "image";
                    this.currentNode.AddContent(imgContent);
                    this.SetContain(this.currentNode);
                    App.Synchronize();
                   // App.InfoLabel.SetMessage("Drag Image Successful", true);
                    return;
                }
                if (e.Data.GetDataPresent("UnicodeText"))
                {
                    string text = e.Data.GetData(System.Windows.DataFormats.UnicodeText, true).ToString();
                    if (text.Contains("http"))
                    {
                        if (text.Contains("wikipedia"))
                        {
                            HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(App.currentUrl);
                            myRequest.Method = "GET";
                            WebResponse myResponse = myRequest.GetResponse();
                            StreamReader sr = new StreamReader(myResponse.GetResponseStream(), System.Text.Encoding.UTF8);
                            string result = sr.ReadToEnd();
                            sr.Close();
                            myResponse.Close();
                            MatchCollection matchCollection = Regex.Matches(result, "img.*?" + "src=.*?" + System.IO.Path.GetFileName(text).Replace(" ", "_").Replace(",", "%2C"));
                            if (matchCollection.Count != 0)
                            {
                                string link = "http:" + matchCollection[0].Value.Split('"').Last().Replace("thumb/", "");
                                System.Drawing.Image img = DownloadImage(link);
                                if (img != null)
                                {
                                    DateTime inow = DateTime.Now;
                                    string inewPath = App.ProjectDirectory+"/Images" +"/" + "abc" + inow.Second.ToString() + inow.Minute.ToString() + inow.Hour.ToString() + inow.Day.ToString() + inow.Month.ToString() + inow.Year.ToString() + ".png";
                                    img.Save(inewPath);
                                    //int returnID = ResourcesHelper.GetInstance().AddResource(inewPath);
                                    ImageContent imgContent = new ImageContent(inewPath);

                                    imgContent.Type = "image";
                                    this.currentNode.AddContent(imgContent);
                                    this.SetContain(this.currentNode);
                                    App.Synchronize();
                                    //App.InfoLabel.SetMessage("Drag Image Successful", true);
                                    return;
                                }
                            }
                        }
                        else
                        {
                            HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(App.currentUrl);
                            myRequest.Method = "GET";
                            WebResponse myResponse = myRequest.GetResponse();
                            StreamReader sr = new StreamReader(myResponse.GetResponseStream(), System.Text.Encoding.UTF8);
                            string result = sr.ReadToEnd();
                            sr.Close();
                            myResponse.Close();
                            MatchCollection matchCollection = Regex.Matches(result,text+ ".*?img.*?" + "src=.*?"+'"');
                            if (matchCollection.Count != 0)
                            {
                                string link = "http:" + matchCollection[0].Value.Split('"').Last().Replace("thumb/", "");
                                System.Drawing.Image img = DownloadImage(link);
                                if (img != null)
                                {
                                    DateTime inow = DateTime.Now;
                                    string inewPath = App.ProjectDirectory+"/Images" + "/" + "abc" + inow.Second.ToString() + inow.Minute.ToString() + inow.Hour.ToString() + inow.Day.ToString() + inow.Month.ToString() + inow.Year.ToString() + ".png";
                                    img.Save(inewPath);
                                   // int returnID = ResourcesHelper.GetInstance().AddResource(inewPath);
                                    ImageContent imgContent = new ImageContent(inewPath);

                                    imgContent.Type = "image";
                                    this.currentNode.AddContent(imgContent);
                                    this.SetContain(this.currentNode);
                                    App.Synchronize();
                                   // App.InfoLabel.SetMessage("Drag Image Successful", true);
                                    return;
                                }
                            }
                        }
                        
                    }
                   
                    
                        TextContent txtContent = new TextContent();

                        System.Windows.Forms.RichTextBox rtb = new System.Windows.Forms.RichTextBox();
                        rtb.Text = text;
                        string outputText = rtb.Rtf;
                        DataLayer.Paragraph para = new DataLayer.Paragraph(outputText);
                        txtContent.Type = "text";
                        txtContent.ListParagraph.Add(para);
                        currentNode.AddContent(txtContent);
                        App.Synchronize();
                        this.SetContain(this.currentNode);
                        //App.InfoLabel.SetMessage("Drop Text Succesful", true);
                    
                    return ;
                }
                //App.InfoLabel.SetMessage("Can not drag", false);
                return ;
            
        }
        public System.Drawing.Image DownloadImage(string _URL)
        {
            System.Drawing.Image _tmpImage = null;

            try
            {
                // Open a connection
                System.Net.HttpWebRequest _HttpWebRequest = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(_URL);

                _HttpWebRequest.AllowWriteStreamBuffering = true;

                // You can also specify additional header values like the user agent or the referer: (Optional)
                _HttpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727)";
                _HttpWebRequest.Referer = "http://www.google.com/";

                // set timeout for 20 seconds (Optional)
                _HttpWebRequest.Timeout = 20000;

                // Request response:
                System.Net.WebResponse _WebResponse = _HttpWebRequest.GetResponse();

                // Open data stream:
                System.IO.Stream _WebStream = _WebResponse.GetResponseStream();

                // convert webstream to image
                _tmpImage = System.Drawing.Image.FromStream(_WebStream);
                // Cleanup
                _WebResponse.Close();
                _WebResponse.Close();
            }
            catch (Exception _Exception)
            {
                // Error
                Console.WriteLine("Exception caught in process: {0}", _Exception.ToString());
                return null;
            }

            return _tmpImage;
        }

        private static Bitmap CreateBitmap(BitmapSource source)
        {
            using (var memoryStream = new MemoryStream())
            {
                var bitmapEncoder = new BmpBitmapEncoder();
                bitmapEncoder.Frames.Add(BitmapFrame.Create(source));
                bitmapEncoder.Save(memoryStream);
                memoryStream.Position = 0;
                return new Bitmap(memoryStream);
            }
        }

        private static BitmapSource CreateBitmapSource(Bitmap bitmap)
        {
            IntPtr hBitmap = bitmap.GetHbitmap();
            BitmapSource bmpSrc = Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero,
              Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
            DeleteObject(hBitmap);
            return bmpSrc;
        }

        [DllImport("gdi32.dll")]
        private static extern bool DeleteObject(IntPtr hObject);

        private void Grid_DragOver(object sender, System.Windows.DragEventArgs e)
        {
            int i = 0;
            i = 2;
            string s = e.ToString();
        }

        private void MyListBox_PreviewDrop(object sender, System.Windows.DragEventArgs e)
        {
            e.Effects = System.Windows.DragDropEffects.Copy;
        }

        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            if (System.Windows.Clipboard.ContainsText())
            {
                string text = System.Windows.Clipboard.GetData(System.Windows.DataFormats.UnicodeText).ToString();
                TextContent textContent = new TextContent();

                textContent.Type = "text";
                System.Windows.Forms.RichTextBox rtf=new System.Windows.Forms.RichTextBox();
                rtf.Text=text;
                string output=rtf.Rtf;
                DataLayer.Paragraph para=new DataLayer.Paragraph(output);
                textContent.ListParagraph.Add(para);
                currentNode.AddContent(textContent);
                this.SetContain(currentNode);
            }
            if (System.Windows.Forms.Clipboard.ContainsImage())
            {
                try
                {
                    System.Drawing.Image img = System.Windows.Forms.Clipboard.GetImage();
                    DateTime inow = DateTime.Now;
                    string newPath = App.ProjectDirectory + "/Images" + "/" + "abc" + inow.Second.ToString() + inow.Minute.ToString() + inow.Hour.ToString() + inow.Day.ToString() + inow.Month.ToString() + inow.Year.ToString() + ".bmp";
                    img.Save(newPath, ImageFormat.Bmp);
                    //DataLayer.ResourcesHelper.GetInstance().AddResource(newPath);
                    //int id = DataLayer.ResourcesHelper.GetInstance().PathToID(newPath);
                    ImageContent imgContent = new ImageContent(newPath);
                    imgContent.Type = "Image";
                    //imgContent.ResourceID = id;
                    currentNode.AddContent(imgContent);
                    this.SetContain(currentNode);
                }
                catch { }
            }
        }
        private System.Drawing.Bitmap BitmapFromSource(BitmapSource bitmapsource)
        {
            System.Drawing.Bitmap bitmap;
            using (MemoryStream outStream = new MemoryStream())
            {
                BitmapEncoder enc = new BmpBitmapEncoder();
                enc.Frames.Add(BitmapFrame.Create(bitmapsource));
                enc.Save(outStream);
                bitmap = new System.Drawing.Bitmap(outStream);
            }
            return bitmap;
        }


        private void MyListBox_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
         
        }
        private void ScrollViewer_PreviewKeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if ((e.Key == Key.V) && Keyboard.IsKeyDown(Key.LeftCtrl))
            {
                MenuItem_Click(null, null);
            }

        }

        private void Image_ImageFailed(object sender, ExceptionRoutedEventArgs e)
        {

        }

        private void BrowseWebForImages_Click(object sender, RoutedEventArgs e)
        {
            AvalonDock.DocumentContent doc = new DocumentContent();
            if (currentNode != null)
                doc.Title = "Search For:" + currentNode.Name;
            else
                doc.Title = "Search Image";
            WebControl web = new WebControl();
            string url = "www.google.com.vn/search?hl=vi&q=***&bav=on.2,or.r_gc.r_pw.r_cp.r_qf.,cf.osb&biw=1173&bih=802&um=1&ie=UTF-8&tbm=isch&source=og&sa=N&tab=wi&ei=C6zVT_vdH6u4iAeFubiMAw";  
            url=url.Replace("***", currentNode.Name.Replace(" ","+"));
            web.Navigate(url);
            doc.Content = web;
            dockPane.Items.Add(doc);
            dockPane.SelectedItem = doc;
        }

    }
}
